package com.teyao.teyao_back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teyao.teyao_back.config.myPage;
import com.teyao.teyao_back.entity.ClientDeliverWrapper;
import com.teyao.teyao_back.service.ClientDeliverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/clientDeliver")
public class ClientDeliverController {
    @Autowired
    private ClientDeliverService clientDeliverService;

    @GetMapping("/selectAll")
    public myPage selectAll(Integer currentPage) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);

        IPage result = clientDeliverService.page(page);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }


    @GetMapping("/selectByUser")
    public myPage selectByUser(Integer currentPage, Integer userId) {

        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> clientDeliverWrapperQueryWrapper = new QueryWrapper<>();
        clientDeliverWrapperQueryWrapper.eq("user_id", userId).ne("status", "已签收").ne("status", "已退库");
        IPage result = clientDeliverService.page(page, clientDeliverWrapperQueryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //用户的历史订单
    @GetMapping("/UserHistory")
    public myPage UserHistory(Integer currentPage, Integer userId, String orderId) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> clientDeliverWrapperQueryWrapper = new QueryWrapper<>();
        if (orderId != null && !orderId.isBlank()) {
            clientDeliverWrapperQueryWrapper.like("order_id", orderId.trim());
        }
        clientDeliverWrapperQueryWrapper.eq("user_id", userId).eq("status", "已签收").or().eq("user_id", userId).eq("status", "已退库");
        IPage result = clientDeliverService.page(page, clientDeliverWrapperQueryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //用户的订单
    @GetMapping("/selectReturn")
    public myPage selectReturn(Integer currentPage) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "退货中");
        IPage result = clientDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    @GetMapping("/selectNotStart")
    public myPage selectNotStart(Integer currentPage) {

        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未配货");
        IPage result = clientDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
//        return clientDeliverService.selectNotStart();
    }

    //id查询
    @GetMapping("/selectByOrderId")
    public myPage selectByOrderId(Integer currentPage, String orderId) {

        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("order_id", orderId.trim());
        IPage result = clientDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //客户的id查询
    @GetMapping("/selectByOrderIdClient")
    public myPage selectByOrderIdClient(Integer currentPage, String orderId, Integer userId) {
        System.out.println(userId + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("order_id", orderId.trim()).eq("user_id", userId);
        IPage result = clientDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }


    //客户名称查询
    @GetMapping("/selectByClient")
    public myPage selectByClient(Integer currentPage, String client) {

        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("client", client);
        IPage result = clientDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //状态查询
    @GetMapping("/selectByStatus")
    public myPage selectByStatus(Integer currentPage, String status) {

        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        IPage result = clientDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }


    //状态查询
    @GetMapping("/selectNotDriving")
    public myPage selectNotDriving(Integer currentPage) {

        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未配送");
        IPage result = clientDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }


    //    不定查询
    @GetMapping("/myQuery")
    public myPage MyQuery(Integer currentPage, Integer size, ClientDeliverWrapper clientDeliverWrapper) {
        System.out.println(clientDeliverWrapper + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
        return clientDeliverService.MyQuery(currentPage, size, clientDeliverWrapper);
    }

    //客户页面不定查询
    @GetMapping("/myQueryClient")
    public myPage MyQueryClient(Integer currentPage, Integer size, Integer userId, ClientDeliverWrapper clientDeliverWrapper) {
        System.out.println(clientDeliverWrapper + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
        return clientDeliverService.MyQueryClient(currentPage, size, userId, clientDeliverWrapper);
    }


    //王
    @GetMapping("/page")
    public myPage page(ClientDeliverWrapper clientDeliverWrapper, Integer pageSize, long current, String statu) {
        return clientDeliverService.getByExample(clientDeliverWrapper, pageSize, current, statu);
    }

}
